package com.grandtech.insurance.common.oss;

import com.google.gson.Gson;
import com.grandtech.insurance.common.utils.GsonFactory;

import java.io.Serializable;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Objects;

public class AliOssClientParamDO implements Serializable {

    String endpoint;
    String bucket;
    String accessKeyId;
    String accessKeySecret;
    String path;

    public AliOssClientParamDO() {
    }

    public AliOssClientParamDO(String _endpoint, String _bucket, String _accessKeyId) {
        endpoint = _endpoint;
        bucket = _bucket;
        accessKeyId = _accessKeyId;
    }

    public AliOssClientParamDO(String _endpoint, String _bucket, String _accessKeyId, String _accessKeySecret) {
        endpoint = _endpoint;
        bucket = _bucket;
        accessKeyId = _accessKeyId;
        accessKeySecret = _accessKeySecret;
    }

    public String getBucket() {
        return bucket;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getBucketName() {
        return bucket;
    }

    public void setBucketName(String bucketName) {
        this.bucket = bucketName;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof AliOssClientParamDO)) {
            return false;
        }
        AliOssClientParamDO that = (AliOssClientParamDO) o;
        return Objects.equals(endpoint, that.endpoint) &&
                Objects.equals(bucket, that.bucket) &&
                Objects.equals(accessKeyId, that.accessKeyId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(endpoint, bucket, accessKeyId);
    }

    public String encode() {
        if (this == null) {
            return null;
        }
        Gson gson = GsonFactory.getFactory().getComGson();
        String json = gson.toJson(this);
        try {
            return URLEncoder.encode(json, "UTF-8");
        } catch (Exception e) {
            return null;
        }
    }


    public static AliOssClientParamDO decode(String o) {
        try {
            Gson gson = GsonFactory.getFactory().getComGson();
            String json = URLDecoder.decode(o, "UTF-8");
            AliOssClientParamDO comHeader = gson.fromJson(json, AliOssClientParamDO.class);
            if (comHeader.getAccessKeyId() == null
                    || comHeader.getAccessKeyId().equals("")
                    || comHeader.getBucketName() == null
                    || comHeader.getBucketName().equals("")
                    || comHeader.getEndpoint() == null
                    || comHeader.getEndpoint().equals("")) {
                return null;
            }
            return comHeader;
        } catch (Exception e) {
            return null;
        }
    }

}
